package com.ruoyi.web.controller.lwpq;

import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.web.controller.system.SysProfileController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.lwpq.domain.LwpqJl;
import com.ruoyi.lwpq.service.ILwpqJlService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

/**
 * 劳务派遣简历管理Controller
 * 
 * @author ruoyi
 * @date 2022-03-01
 */
@Controller
@RequestMapping("/lwpq/jl")
public class LwpqJlController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(LwpqJlController.class);
    private String prefix = "lwpq/jl";

    @Autowired
    private ILwpqJlService lwpqJlService;


//    此处可以直接由前端传入头像地址,例如下方
    @GetMapping("/avatar")
    public String avatar(@RequestParam(required = false) String url,ModelMap mmap)
    {
        if (StringUtils.isNotEmpty(url)){
            mmap.put("avatar",url);
        }

        return prefix + "/avatar";
    }

    /**
     * 保存头像
     */
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PostMapping("/updateAvatar")
    @ResponseBody
    public AjaxResult updateAvatar(@RequestParam("avatarfile") MultipartFile file)
    {
        try
        {
            if (!file.isEmpty())
            {
                String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);
                return success(avatar);
            }
            return error();
        }
        catch (Exception e)
        {
            log.error("修改头像失败！", e);
            return error(e.getMessage());
        }
    }

    @RequiresPermissions("lwpq:jl:view")
    @GetMapping()
    public String jl()
    {
        return prefix + "/jl";
    }

    /**
     * 查询劳务派遣简历管理列表
     */
    @RequiresPermissions("lwpq:jl:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LwpqJl lwpqJl)
    {
        startPage();
        lwpqJl.setCreateBy(getLoginName());
        List<LwpqJl> list = lwpqJlService.selectLwpqJlList(lwpqJl);
        return getDataTable(list);
    }


    /**
     * 导出劳务派遣简历管理列表
     */
    @RequiresPermissions("lwpq:jl:export")
    @Log(title = "劳务派遣简历管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LwpqJl lwpqJl)
    {
        List<LwpqJl> list = lwpqJlService.selectLwpqJlList(lwpqJl);
        ExcelUtil<LwpqJl> util = new ExcelUtil<LwpqJl>(LwpqJl.class);
        return util.exportExcel(list, "劳务派遣简历管理数据");
    }

    /**
     * 新增劳务派遣简历管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存劳务派遣简历管理
     */
    @RequiresPermissions("lwpq:jl:add")
    @Log(title = "劳务派遣简历管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LwpqJl lwpqJl)
    {
        lwpqJl.setCreateBy(getLoginName());
        return toAjax(lwpqJlService.insertLwpqJl(lwpqJl));
    }

    /**
     * 修改劳务派遣简历管理
     */
    @RequiresPermissions("lwpq:jl:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        LwpqJl lwpqJl = lwpqJlService.selectLwpqJlById(id);
        mmap.put("lwpqJl", lwpqJl);
        return prefix + "/edit";
    }

    /**
     * 修改保存劳务派遣简历管理
     */
    @RequiresPermissions("lwpq:jl:edit")
    @Log(title = "劳务派遣简历管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LwpqJl lwpqJl)
    {
        lwpqJl.setUpdateBy(getLoginName());
        return toAjax(lwpqJlService.updateLwpqJl(lwpqJl));
    }

    /**
     * 删除劳务派遣简历管理
     */
    @RequiresPermissions("lwpq:jl:remove")
    @Log(title = "劳务派遣简历管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(lwpqJlService.deleteLwpqJlByIds(ids));
    }



    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("lwpq:jl:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<LwpqJl> util = new ExcelUtil<LwpqJl>(LwpqJl.class);
        List<LwpqJl> lwpqJl = util.importExcel(file.getInputStream());
        String message = lwpqJlService.importUser(lwpqJl, updateSupport, getLoginName());
        return AjaxResult.success(message);
    }

    @RequiresPermissions("lwpq:jl:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<LwpqJl> util = new ExcelUtil<LwpqJl>(LwpqJl.class);

        return util.importTemplateExcel("简历数据");
    }
}
